package com.xyy.saas.payment.adpater.jdCredit.req;

import com.jddglobal.open.request.base.JddRequest;
import com.jddglobal.open.support.util.JacksonUtils;
import com.xyy.saas.payment.adpater.jdCredit.resp.SmapiResponse;
import com.xyy.saas.payment.common.Constants;
import com.xyy.saas.payment.payment.core.dto.RefundDto;
import com.xyy.saas.payment.util.AmountCastUtil;
import com.xyy.saas.payment.util.ApolloUtil;
import com.xyy.saas.payment.util.DateUtil;
import lombok.Data;
import org.apache.http.client.utils.DateUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

/**
 * 退款申请请求参数
 *
 * @author yuchengying
 * @date 2024/1/4
 */
//@Deprecated
@Data
public class RefundApplyReq implements JddRequest<SmapiResponse> {
    /**
     * 请求编号
     */
    private String reqNo;
    /**
     * 请求时间
     */
    private String reqDate;
    /**
     * 产品编号不能为空
     */
    private String productCode;
    /**
     * 合作方编号
     */
    private String partnerCode;
    /**
     * 业务编号
     */
    private String bizNo;
    /**
     * 经销商编码
     */
    private String custCode;
    /**
     * 结算主体编号
     */
    private String settleUserCode;
    /**
     * 订单编号
     */
    private String orderCode;
    /**
     * 退款原因
     */
    private String refundDesc;
    /**
     * 退款金额
     */
    private BigDecimal refundAmount;
    /**
     * 支付账户号
     */
    private String payAccountNo;
    /**
     * 支付账户名
     */
    private String payAccountName;
    /**
     * 支付账户开户行
     */
    private String payOpenBank;
    /**
     * 用户收款账户号
     */
    private String receiveAccountNo;
    /**
     * 用户收款账户号
     */
    private String receiveAccountName;
    /**
     * 退款结果通知地址
     */
    private String notifyUrl;
    /**
     * 退款支付方式
     */
    private String refundPayWay;
    /**
     * 风控参数
     */
    private Map<String, Object> riskControlParams;

    public static RefundApplyReq from(RefundDto refundDto, String reqNo) {
        RefundApplyReq applyReq = new RefundApplyReq();
        applyReq.setReqNo(reqNo);
        applyReq.setReqDate(DateUtils.formatDate(new Date(), Constants.YYYYMMDDHHMMSS));
        applyReq.setProductCode(Constants.JD_CREDIT_PRODUCT_CODE);
        applyReq.setPartnerCode(Constants.JD_CREDIT_PARTNER_CODE);
        applyReq.setBizNo(refundDto.getRefundNo());
        applyReq.setCustCode(refundDto.getPayer());
        //非必填
//        applyReq.setSettleUserCode();
        applyReq.setOrderCode(refundDto.getPayNo());
        applyReq.setRefundDesc(refundDto.getDescription());
        applyReq.setRefundAmount(AmountCastUtil.castFen2YuanByBigDecimal(refundDto.getAmount()));
        applyReq.setPayAccountNo(ApolloUtil.getProperty(Constants.JD_CREDIT_REFUND_PAY_ACCOUNT_KEY));
        applyReq.setPayAccountName(ApolloUtil.getProperty(Constants.JD_CREDIT_REFUND_PAY_ACCOUNT_NAME_KEY));
        applyReq.setPayOpenBank(ApolloUtil.getProperty(Constants.JD_CREDIT_REFUND_PAY_OPEN_BANK_KEY));
        applyReq.setReceiveAccountNo(Constants.RECEIVE_ACCOUNT_NO);
        applyReq.setReceiveAccountName(Constants.RECEIVE_ACCOUNT_NAME);
        applyReq.setNotifyUrl(ApolloUtil.getProperty(Constants.JD_CREDIT_REFUND_NOTIFY_KEY));
        applyReq.setRefundPayWay(Constants.JD_CREDIT_REFUND_PAY_WAY);
//        applyReq.setRiskControlParams();
        return applyReq;
    }

    @Override
    public String getApiMethod() {
        return "/smapi/v1/nav/refundApply";
    }

    @Override
    public String getRequestTime() {
        return DateUtils.formatDate(new Date(), "yyyyMMddHHmmssSSS");
    }

    @Override
    public Class getResponseClass() {
        return SmapiResponse.class;
    }

    @Override
    public String getJsonParams() {
        return JacksonUtils.toJson(this);
    }
}
